package manager.web.contrl;

import javax.servlet.http.HttpServletRequest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import manager.domain.SessionParamEnum;
import manager.web.domain.IndexResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pub.permission.domain.*;
import pub.permission.service.ExtSysResourceService;
import pub.permission.service.ResourceService;
import pub.sys.service.SysCodeItemService;
import pub.user.domain.Account;
import pub.user.domain.LoginResult;
import pub.user.domain.User;
import pub.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 *
 * @author 002465
 * @created 2017/5/3 21:48
 * @return ${return_type}
 * @since v1.0.0
 * ${tags}
 */
@Controller
public class LoginCtrl {
    @Autowired
    private UserService userService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private SysCodeItemService sysCodeItemService;
    @Autowired
    private ExtSysResourceService extSysResourceService;

    @RequestMapping(value = "/login")
    public ModelAndView login(Account account,ModelMap modelMap, HttpServletRequest request) {
        String ctx = request.getContextPath();
        modelMap.put("ctx", ctx);
        modelMap.put("staticPath", ctx+"/static");
        ModelAndView model = new ModelAndView("view/index");
        if (account == null
                || (StringUtils.isEmpty(account.getLoginName()) && StringUtils
                .isEmpty(account.getPassWord()))) {
            model.setViewName("view/login");
            return model;
        }
        if (StringUtils.isNotEmpty(account.getPassWord()))
            account.encryptPass();
        LoginResult loginResult = userService.login(account);

        if(!account.getLoginName().equals("admin")){
            model.addObject("message", "只有admin才有权限");
            model.setViewName("view/login");
        }else if (!loginResult.isSuccess()) {
            model.addObject("message", "密码错误!");
            model.setViewName("view/login");
        } else {
           /* User user = userService.getUserByLoginName(account.getLoginName());
            request.getSession().setAttribute(SessionParamEnum.USER.getValue(),
                    user);
            SysCodeItem codeItem = sysCodeItemService
                    .querySysCodeItemByCodeType(SysCode.ROLE_SWITCH);
            List<Resource> resources;
            // 权限是否全部开放
            if (codeItem == null
                    || codeItem.getValue().equals(
                    SysCodeRoleSwitch.OPEN.getValue())) {
                resources = resourceService.getAllResources();
            } else {
                // 权限菜单初始化
                resources = resourceService.getResourceTree(user);
            }*/
            List<ExtSysResource> extSysResourceList = extSysResourceService.getManageMenuList();
            Gson gson = new GsonBuilder().serializeNulls().create();
            IndexResult result = getResult(extSysResourceList);
            modelMap.put("result", gson.toJson(result));
        }
        return model;
    }
    private IndexResult getResult(List<ExtSysResource> extSysResourceList){
        IndexResult result = new IndexResult();
       /* Resource rootNode = resourceService.getRootNodeByAccount(account);
        result.setMenuData(rootNode);*/
        ExtSysResource rootNode = new ExtSysResource();
        rootNode.setChildren(extSysResourceList);
        result.setMenuData(rootNode);
        return result;
    }
    private List<ExtSysResource> getAllExtResources(List<Resource> resources){
        //extSysResourceService.saveOrUpdate(getAllExtResources(resourceService.getAllResources()));
        List<ExtSysResource> extSysResourceList = new ArrayList<>();
        for (Resource resource:resources) {
            ExtSysResource extSysResource = new ExtSysResource();
            extSysResource.setMenuUrl(resource.getResourceStr());
            extSysResource.setName(resource.getName());
            if(resource.getName().equals("资源管理")||resource.getName().equals("用户管理")||resource.getName().equals("角色管理")) {
                extSysResource.setIsManageMenu(true);
            }else{
                extSysResource.setIsManageMenu(false);
            }
            extSysResource.setMemo(resource.getMemo());
            extSysResource.setDisOrder(resource.getDisOrder());
            extSysResource.setCss1(resource.getClass1());
            extSysResource.setCss2(resource.getClass2());
            extSysResourceList.add(extSysResource);
        }
        return extSysResourceList;
    }
}
